<?php declare(strict_types=1);

namespace App\Services\Count;


use App\Models\Store;
use App\Models\StoreWaysDesc;
use App\Models\User;

class LoadRelationDataService
{

    /**
     * @param array $data 必须包含: store_id
     * @return void
     */
    public function getStores(array $data): array
    {
        $storeIds = array_column($data, "store_id");

        $stores = Store::query()->whereIn("store_id", $storeIds)->get()->toArray();

        $storeHashMap = array_combine(array_column($stores, "store_id"), $stores);

        return $storeHashMap;
    }

    /**
     * @param array $data 必须包含:   ways_type
     * @return array
     */
    public function getStoreWaysDescs(array $data): array
    {
        $ways_type = array_column($data, "ways_type");

        $storeWaysDesc = StoreWaysDesc::query()->whereIn("ways_type", $ways_type)->get()->toArray();

        $storeWaysDescHashMap = array_combine(array_column($storeWaysDesc, "ways_type"), $storeWaysDesc);

        return $storeWaysDescHashMap;
    }

    /**
     * @param array $data 必须包含公司名
     * @return array
     */
    public function getCompanys(array $data): array
    {
        $companys = array_column($data, "company");

        $companyData = StoreWaysDesc::query()->select(["company", "company_name"])
            ->where("company", $companys)
            ->groupBy(["company", "company_name"])
            ->get()->toArray();

        $companyHashMap = array_combine(array_column($companyData, "company"), $companyData);

        return $companyHashMap;
    }


    protected $iDColumn = ["user_id", "oem_id", "agent_id", "agent1_id", "agent2_id", "agent3_id", "agent4_id"];

    /**
     * @return array 包含 user_id   oem_id agent_id  agent1_id agent2_id agent3_id  agent4_id
     */
    public function getUsers(array $data): array
    {
        $userIds = [];
        foreach ($data as $vs) {
            foreach ($vs as $column => $value) {
                if (in_array($column, $this->iDColumn)) {
                    array_push($userIds, $value);
                }
            }
        }
        $users = User::query()->select(["id", 'pid_name', "pid",
            'level',
            'level_name',
            'name',
            'logo',
            'phone',])->whereIn("id", $userIds)->get()->toArray();

        $userHashMap = array_combine(array_column($users, "id"), $users);

        return $userHashMap;
    }

    public function getModels(int $model): string
    {
        $data = [
            1 => "服务费模式",
            2 => "代扣模式",
            3 => "通道模式",
            4 => "预充模式"
        ];
        return $data[$model] ?? "";
    }

    /**
     * @param array $data
     * @param array $model 合并数据 1 getStores  2 getStoreWaysDescs 3 getCompanys 4 getUsers 5 model
     * @return void
     */
    public function mergeData(array $data, array $model = []): array
    {
        $modelCollection = [];
        if (empty($model)) {
            return [$data, $modelCollection];
        }
        $stores = [];
        $storeWaysDescs = [];
        $companys = [];
        $users = [];
        //处理model合并的数据
        foreach ($model as $m) {
            switch ($m) {
                case 1:
                    $stores = $this->getStores($data);
                    $modelCollection[1] = $stores;
                    break;
                case 2:
                    $storeWaysDescs = $this->getStoreWaysDescs($data);
                    $modelCollection[2] = $storeWaysDescs;
                    break;
                case 3:
                    $companys = $this->getCompanys($data);
                    $modelCollection[3] = $companys;
                    break;
                case 4:
                    $users = $this->getUsers($data);
                    $modelCollection[4] = $users;
                    break;
            }
        }

        //处理合并的数据
        foreach ($data as $k => $v) {
            if (isset($v["receive_model"])) {
                $v["receiveModelText"] = $this->getModels((int)$v["receive_model"]);
            }
            foreach ($model as $m) {
                switch ($m) {
                    case 1://合并store数据
                        $v["storeAttr"] = null;
                        if (!empty($stores) && isset($v["store_id"])) {
                            $v["storeAttr"] = $stores[$v["store_id"]] ?? null;
                        }
                        break;
                    case 2://合并 getStoreWaysDescs
                        $v["storeWaysDescAttr"] = null;
                        if (!empty($storeWaysDescs) && isset($v["ways_type"])) {
                            $v["storeWaysDescAttr"] = $storeWaysDescs[$v["ways_type"]] ?? null;
                        }
                        break;
                    case 3://合并 companys
                        $v["companyAttr"] = null;
                        if (!empty($companys) && isset($v["company"])) {
                            $v["companyAttr"] = $companys[$v["company"]] ?? null;
                        }
                        break;
                    case 4://合并 user
                        foreach ($this->iDColumn as $col) {
                            $v[rtrim($col, "_id") . "Attr"] = null;
                            if (empty($users) && isset($v[$col])) {
                                $v[$col . "Attr"] = $users[$v[$col]] ?? null;
                            }
                        }
                        break;
                }
            }
            $data[$k] = $v;
        }
        return [$data, $modelCollection];
    }


}